package br.com.jspl.app.ui;
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;

import br.com.jspl.app.devices.*;

class SimuladorInterface extends JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6749888656261738997L;
	ProtoBoard myProtoBoard ;
	private PortaXor pXor;
	private PortaOR pOr;
	private PortaAND pAnd;
	private PortaNot pNot;	
	private FontePulsadora Fonte; 
	private Cabo caboBF;
	private Led myLed;
	private FileChoose filechoose;
	private Delete mydelete;
	private JToolBar PainelFerramentas;
	
	SimuladorInterface() {
		//Deixa o Frame com icone :=)
		Toolkit tk = Toolkit.getDefaultToolkit();
		Image comp = tk.getImage(getClass().getClassLoader().getResource("br/com/jspl/imagens/PAnd.png"));
		setIconImage(comp);
		setTitle("JSPL - Simulador de Portas L\u00f3gicas - 3.1");
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		//Define o Layout do JFRame para borda
		setLayout(new BorderLayout());
		
		// Define o tamanho do JFrame
		setSize(800, 600);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		//Cria o painel onde do canto esquerdo, onde ficaram os componentes
		PainelFerramentas = new JToolBar();		

		//Adiciona o Painel criado no Frame atual
		getContentPane().add(PainelFerramentas, BorderLayout.NORTH);
		myProtoBoard = new ProtoBoard();		
		myProtoBoard.reiniciaPonteiro();

		//PainelFerramentas.setPonteiro(Ponteiro);
		getContentPane().add(myProtoBoard, BorderLayout.CENTER);		
		
		pAnd = new PortaAND("Porta AND", "PortaAndT.png", "PortaAndIco.png",myProtoBoard);
        pOr  = new PortaOR("Porta OR", "PortaOrT.png", "PortaOrIco.png",myProtoBoard);
        pNot = new PortaNot("Porta Not", "PortaNotT.png", "PortaNotIco.png",myProtoBoard);
        pXor = new PortaXor("Porta XOR", "PortaXorT.png", "PortaXorIco.png",myProtoBoard);
        caboBF = new Cabo("Cabo","CaboIco.png","CaboIco.png",myProtoBoard);
        Fonte =  new FontePulsadora("Fonte Pulsadora", "GeradorCinza.png", "GeradorIco.png","GeradorAzul.png",myProtoBoard);
        myLed = new Led("Led", "TerminalTCinza.png", "TerminalIco.png","TerminalTVermelho.png",myProtoBoard);
        mydelete = new Delete("Apagar Componete","DeleteIco.png","DeleteIco.png",myProtoBoard);
        filechoose = new FileChoose(myProtoBoard);
        PainelFerramentas.add(filechoose.getOpenButton());
		PainelFerramentas.add(filechoose.getSaveButton());
		PainelFerramentas.add(mydelete.getBotao());
        PainelFerramentas.add(pAnd.getBotao());
		PainelFerramentas.add(pOr.getBotao());
		PainelFerramentas.add(pNot.getBotao());
		PainelFerramentas.add(pXor.getBotao());
		PainelFerramentas.add(caboBF.getBotao());
		PainelFerramentas.add(Fonte.getBotao());
		PainelFerramentas.add(myLed.getBotao());    
		 		 
		//Cria os Menus
        JMenuBar barraMenu = new JMenuBar();

		/**
		 * Menu Arquivo
		 */
		JMenu MenuArquivo = new JMenu("Arquivo");
		MenuArquivo.setMnemonic('A');
		
		JMenuItem MenuArqNovo = new JMenuItem("Novo", 'N');
		MenuArqNovo.addActionListener(this);
		
		JMenuItem MenuArqSair = new JMenuItem("Sair", 'R');
		MenuArqSair.addActionListener(this);
		
		//Define a tecla de atalho
		MenuArqNovo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
		
		MenuArquivo.add(MenuArqNovo);
		MenuArquivo.add(MenuArqSair);
		MenuArquivo.add(filechoose.getAcaoOpen());		
		MenuArquivo.add(filechoose.getAcaoSave());		
		MenuArquivo.add(MenuArqSair);
		barraMenu.add(MenuArquivo);
		
		/**
		 * Menu Editar
		 */
		/*
		JMenu MenuEditar = new JMenu("Editar");
		MenuEditar.setMnemonic('E');
		JMenuItem MenuEditarOp[]= new JMenuItem[1]; 
		MenuEditarOp[0] = new JMenuItem("Selecionar Componente");
		barraMenu.add(makeMenu(MenuEditar,MenuEditarOp , this));
		*/
		/**
		 * Menu Ao
		 */
		JMenu MenuSimular = new JMenu("A\u00e7\u00e3o");
		MenuSimular.setMnemonic('S');		
		barraMenu.add(makeMenu(MenuSimular,
				new Object[] { "Ligar", "Desligar" }, this));
		
		/**
		 * Menu Inserir Componente
		 */		
		JMenu MenuInserirComp = new JMenu("Inserir Componente");
		MenuInserirComp.add(pAnd.getAcao());
		MenuInserirComp.add(pOr.getAcao());
		MenuInserirComp.add(pNot.getAcao());
		MenuInserirComp.add(pXor.getAcao());
		MenuInserirComp.add(caboBF.getAcao());
		MenuInserirComp.add(Fonte.getAcao());
		MenuInserirComp.add(myLed.getAcao());
		/**
		 * Ativa Menu
		 */
		barraMenu.add(MenuInserirComp);
		setJMenuBar(barraMenu);
	}
	
	// Trata os Eventos dos Menus
	public void actionPerformed(ActionEvent evt) {
		Circuito c;
		c = myProtoBoard.getCircuitoAtual();
		if (evt.getSource() instanceof JMenuItem) {
			String arg = evt.getActionCommand();
			if (arg == "Sair") {//Bloco que sai
				if (c.sair()) System.exit(0);
			}
			if( arg == "Ligar")											
				c.ligaCircuito();
			if( arg == "Desligar")											
				c.desligaCircuito(); 
			if( arg == "Novo") {
				c=myProtoBoard.getCircuitoAtual();				
				c.novo();
			}
		}
	}
	
//	Funcoes responsaveis para um melhor controle da cricaos de menus    
	public static JMenuItem makeMenuItem(Object item, Object target) {
		JMenuItem r = null;
		if (item instanceof String)
			r = new JMenuItem((String) item);
		else if (item instanceof JMenuItem)
			r = (JMenuItem) item;
		else
			return null;
		if (target instanceof ActionListener)
			r.addActionListener((ActionListener) target);
		return r;
	}

	public static JMenu makeMenu(Object parent, Object[] items, Object target) {
		JMenu m = null;
		if (parent instanceof JMenu)
			m = (JMenu) parent;
		else if (parent instanceof String)
			m = new JMenu((String) parent);
		else
			return null;
		for (int i = 0; i < items.length; i++)
			if (items[i] == null)
				m.addSeparator();
			else
				m.add(makeMenuItem(items[i], target));
		return m;
	}
}
